﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class FrmWeek : Form
    {
        public FrmWeek()
        {
            InitializeComponent();
        }
        //本周收入  支出  收支情况
        decimal thisWeekIn = 0.00M, thisWeekEx = 0.00M, thisWeekInEx = 0.00M;
        //总收支列表
        List<RecordInfo> recordList = new List<RecordInfo>();
        private void FrmWeek_Load(object sender, EventArgs e)
        {

            StatisticsData();
        }

        private void StatisticsData()
        {
            listView1.Items.Clear();
            //加载所有收支记录
            LoadAllRecordList();
            //按周统计收支数据并加载列表
            StatisticsWeekList();
            //加载本周收支数据
            LoadStatInfo();
        }

        /// <summary>
        /// 加载所有收支记录
        /// </summary>
        private void LoadAllRecordList()
        {
            //重置本周统计数据
            thisWeekIn = 0.00M;
            thisWeekEx = 0.00M;
            thisWeekInEx = 0.00M;
            //获取所有的收支记录
            recordList = CommonHelper.GetAllRecordList();
        }


        /// <summary>
        /// 按周统计收支数据并加载列表
        /// </summary>
        private void StatisticsWeekList()
        {
            CultureInfo ci = new CultureInfo("zh-CN");
            List<WeekStatInfo> weekList = new List<WeekStatInfo>();
            //筛选出日期列表
            List<string> listDate = recordList.Select(r => r.RecordTime.ToString("yyyy-MM-dd")).Distinct().ToList();
            //获取年份列表
            List<int> years = listDate.Select(d => DateTime.Parse(d).Year).Distinct().ToList();
            //本周的周数
            int thisWeek = GetWeek(DateTime.Now, ci);
            //按周统计数据列表--遍历年列表---日期列表--周列表 ，统计数据，按周总计
            foreach (int year in years)
            {
                //获取当前年份的日期列表
                var dateList = listDate.Where(d => DateTime.Parse(d).Year == year);
                //筛选出当年的收支记录
                var yrecordList = recordList.Where(r => r.RecordTime.Year == year);
                //获取当前年份的周列表
                List<int> yearWeeks = dateList.Select(d => GetWeek(DateTime.Parse(d), ci)).Distinct().ToList();
                //统计周列表
                foreach (int week in yearWeeks)
                {
                    decimal weekinTotal = 0.00M, weekexTotal = 0.00M, weekinexTotal = 0.00M;
                    //当前周的收支记录
                    var wrecordList = yrecordList.Where(r => GetWeek(r.RecordTime, ci) == week);
                    foreach (var record in wrecordList)
                    {
                        //总计 当前日期的收入和支出
                        if (record.ItemType == "收入")
                        {
                            weekinTotal += record.Money;
                        }
                        else
                        {
                            weekexTotal += record.Money;
                        }
                    }
                    weekinexTotal = weekinTotal - weekexTotal;
                    if (week == thisWeek)//本周的收支情况
                    {
                        thisWeekEx = weekexTotal;
                        thisWeekIn = weekinTotal;
                        thisWeekInEx = weekinexTotal;
                    }
                    //封装周统计数据
                    WeekStatInfo weekInfo = new WeekStatInfo()
                    {
                        Year = year.ToString(),
                        WeekNumber = week.ToString(),
                        InTotal = weekinTotal,
                        ExTotal = weekexTotal,
                        InExTotal = weekinexTotal
                    };
                    weekList.Add(weekInfo);
                }
            }
            //加载数据列表
            if (weekList.Count > 0)
            {
                foreach (WeekStatInfo item in weekList)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = item.Year;
                    li.SubItems.Add(item.WeekNumber);
                    li.SubItems.Add("￥" + item.InTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.ExTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.InExTotal.ToString("0.00"));
                    listView1.Items.Add(li);
                }
            }
        }

        /// <summary>
        /// 本周收支数据加载
        /// </summary>
        private void LoadStatInfo()
        {
            label2.Text = "￥" + thisWeekIn.ToString("0.00") + " 元";
            label5.Text = "￥" + thisWeekEx.ToString("0.00") + " 元";
            label6.Text = "￥" + thisWeekInEx.ToString("0.00") + " 元";
        }

        /// <summary>
        /// 获取当前时间处于第几周
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ci"></param>
        /// <returns></returns>
        private int GetWeek(DateTime dt, CultureInfo ci)
        {
            return ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
        }

  
        private void button3_Click(object sender, EventArgs e)
        {

            StatisticsData();
        } 
    }
}
